package com.example.composecustomthemes.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ProvideTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import com.example.composecustomthemes.customtheme.CustomColors
import com.example.composecustomthemes.customtheme.CustomSpaces
import com.example.composecustomthemes.customtheme.CustomTheme
import com.example.composecustomthemes.customtheme.CustomTypography
import com.example.composecustomthemes.customtheme.LocalColors
import com.example.composecustomthemes.customtheme.LocalSpaces
import com.example.composecustomthemes.customtheme.LocalTypography


fun lightColors() = CustomColors(
    primary = Color(0xFFE67E22),
    text = Color(0xFF000000),
    background = Color(0xFFF5F5F5),
    success = Color(0xFF2ECC71),
    error = Color(0xFFE74C3C),
    isLight = true,
)

fun darkColors() = CustomColors(
    primary = Color(0xFFDF6900),
    text = Color(0xFFFFFFFF),
    background = Color(0xFF353B48),
    success = Color(0xFF44BD32),
    error = Color(0xFFC23616),
    isLight = false,
)

//@Composable
//fun ComposeCustomthemesTheme(
//    darkTheme: Boolean = isSystemInDarkTheme(),
//    // Dynamic color is available on Android 12+
//    dynamicColor: Boolean = true,
//    content: @Composable () -> Unit
//) {
//    val colorScheme = when {
//        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
//            val context = LocalContext.current
//            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
//        }
//
//        darkTheme -> DarkColorScheme
//        else -> LightColorScheme
//    }
//
//    MaterialTheme(
//        colorScheme = colorScheme,
//        typography = Typography,
//        content = content
//    )
//}


// TODO:  参考地址 https://medium.com/@lucasyujideveloper/creating-a-custom-theme-in-jetpack-compose-54cbcbde1ace 对应 git： https://github.com/ujizin/custom-themes
@Composable
fun ComposeCustomTheme(
    spaces: CustomSpaces = CustomTheme.spaces,
    typography: CustomTypography = CustomTheme.typography,
    colors: CustomColors = CustomTheme.colors,
    darkColors: CustomColors? = null,
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val currentColor = remember {
        if (darkColors != null && darkTheme) darkColors else colors
    }

    val rememberedColors = remember {
        currentColor.copy()
    }.apply { updateColorFrom(currentColor) }

    CompositionLocalProvider(
        LocalColors provides rememberedColors,
        LocalSpaces provides spaces,
        LocalTypography provides typography
    ) {
        ProvideTextStyle(typography.body1.copy(color = CustomTheme.colors.text), content = content)
    }

}


//// TODO:  MaterialTheme 源码
//@Composable
//fun MaterialTheme(
//    colorScheme: ColorScheme = MaterialTheme.colorScheme,
//    shapes: Shapes = MaterialTheme.shapes,
//    typography: Typography = MaterialTheme.typography,
//    content: @Composable () -> Unit
//) {
//    val rippleIndication = androidx.compose.material.ripple.rememberRipple()
//    val selectionColors = rememberTextSelectionColors(colorScheme)
//    CompositionLocalProvider(
//        LocalColorScheme provides colorScheme,
//        LocalIndication provides rippleIndication,
//        androidx.compose.material.ripple.LocalRippleTheme provides MaterialRippleTheme,
//        LocalShapes provides shapes,
//        LocalTextSelectionColors provides selectionColors,
//        androidx.compose.material3.LocalTypography provides typography,
//    ) {
//        ProvideTextStyle(value = typography.bodyLarge, content = content)
//    }
//}